using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Windows.Forms.Design;

using Camalon.Drawing;

namespace Camalon.Drawing.Design
{
	/// <summary>
	/// Summary description for DrawingPanel.
	/// </summary>
    [ToolboxItem(false)]
    public class DrawingPad : System.Windows.Forms.UserControl
	{
		private Camalon.Drawing.Design.RulingPanel					_rulingPanel;
		private Camalon.Drawing.Design.Ruler						_hRulerCtrl;
		private Camalon.Drawing.Design.Ruler						_vRulerCtrl;
		private Camalon.Drawing.Design.ShapeComponentTray			_componentTray;
		private Camalon.Drawing.Design.DrawingPad.HeaderSplitter	_splitterTopBottom;  //System.Windows.Forms.Splitter
		private System.Windows.Forms.Panel					_panelTop;

		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

        public DrawingPad(ShapeContainerRootDesignerBase mainDesigner, MenuCommandsHelper mcs)
            : this()
        {

            // This call is required by the Windows.Forms Form Designer.
            _rulingPanel.SetRootDesigner(mainDesigner, mcs);
            _componentTray.SetRootDesigner(mainDesigner, mcs);


        }

		public DrawingPad()
		{
			// This call is required by the Windows.Forms Form Designer.
			SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor,true);
			SetStyle(System.Windows.Forms.ControlStyles.Selectable,true);

			InitializeComponent();
			
			this.AutoScroll = false;
			//this.BackColor  = Color.Transparent;

			_rulingPanel.HRuler = _hRulerCtrl;
			_rulingPanel.VRuler = _vRulerCtrl;
			_vRulerCtrl.RulingPanel = _rulingPanel;
			_hRulerCtrl.RulingPanel = _rulingPanel;

			_componentTray.CloseClick+=new EventHandler(ComponentTray_CloseClick);
			_componentTray.VisibleChanged+=new EventHandler(ComponentTray_VisibleChanged);
			_splitterTopBottom.DoubleClick+=new EventHandler(SplitterTopBottom_DoubleClick);
			_splitterTopBottom.Click+=new EventHandler(SplitterTopBottom_Click);


			//Caution: If one of the ruler become slow, look after the control adding order
			//in the controls collection, which shoel be in the following order:
			//this._panelTop.Controls.Add(this._vRulerCtrl);
			//this._panelTop.Controls.Add(this._hRulerCtrl);
			//this._panelTop.Controls.Add(this._rulingPanel);

		}


		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}


		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this._rulingPanel = new Camalon.Drawing.Design.RulingPanel();
			this._hRulerCtrl = new Camalon.Drawing.Design.Ruler();
			this._vRulerCtrl = new Camalon.Drawing.Design.Ruler();
			this._componentTray = new Camalon.Drawing.Design.ShapeComponentTray();
			this._splitterTopBottom = new Camalon.Drawing.Design.DrawingPad.HeaderSplitter();
			this._panelTop = new System.Windows.Forms.Panel();
			this._panelTop.SuspendLayout();
			this.SuspendLayout();
			// 
			// _rulingPanel
			// 
			this._rulingPanel.AllowDrop = true;
			this._rulingPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this._rulingPanel.AutoScroll = true;
			this._rulingPanel.BackColor = System.Drawing.SystemColors.Window;
			this._rulingPanel.Location = new System.Drawing.Point(20, 20);
			this._rulingPanel.Name = "_rulingPanel";
			this._rulingPanel.Size = new System.Drawing.Size(732, 258);
			this._rulingPanel.TabIndex = 1;
			// 
			// _hRulerCtrl
			// 
			this._hRulerCtrl.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this._hRulerCtrl.BackColor = System.Drawing.SystemColors.Control;
			this._hRulerCtrl.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);
			this._hRulerCtrl.Location = new System.Drawing.Point(19, -1);
			this._hRulerCtrl.Name = "_hRulerCtrl";
			this._hRulerCtrl.Size = new System.Drawing.Size(733, 21);
			this._hRulerCtrl.TabIndex = 0;
			this._hRulerCtrl.Text = "ruler1";
			// 
			// _vRulerCtrl
			// 
			this._vRulerCtrl.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left)));
			this._vRulerCtrl.BackColor = System.Drawing.SystemColors.Control;
			this._vRulerCtrl.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);
			this._vRulerCtrl.Location = new System.Drawing.Point(-1, 19);
			this._vRulerCtrl.Name = "_vRulerCtrl";
			this._vRulerCtrl.Size = new System.Drawing.Size(21, 260);
			this._vRulerCtrl.TabIndex = 3;
			this._vRulerCtrl.Text = "ruler1";
			// 
			// _componentTray
			// 
			this._componentTray.BackColor = System.Drawing.SystemColors.Info;
			this._componentTray.Dock = System.Windows.Forms.DockStyle.Bottom;
			this._componentTray.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this._componentTray.Location = new System.Drawing.Point(0, 282);
			this._componentTray.Name = "_componentTray";
			this._componentTray.Size = new System.Drawing.Size(752, 148);
			this._componentTray.TabIndex = 2;
			// 
			// _splitterTopBottom
			// 
			this._splitterTopBottom.BackColor = System.Drawing.Color.LightGray;
			this._splitterTopBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
			this._splitterTopBottom.DrawToggleButton = false;
			this._splitterTopBottom.Location = new System.Drawing.Point(0, 278);
			this._splitterTopBottom.Name = "_splitterTopBottom";
			this._splitterTopBottom.Size = new System.Drawing.Size(752, 4);
			this._splitterTopBottom.TabIndex = 0;
			this._splitterTopBottom.TabStop = false;
			// 
			// _panelTop
			// 
			this._panelTop.BackColor = System.Drawing.SystemColors.Window;
			this._panelTop.Controls.Add(this._vRulerCtrl);
			this._panelTop.Controls.Add(this._hRulerCtrl);
			this._panelTop.Controls.Add(this._rulingPanel);

			this._panelTop.Dock = System.Windows.Forms.DockStyle.Fill;
			this._panelTop.Location = new System.Drawing.Point(0, 0);
			this._panelTop.Name = "_panelTop";
			this._panelTop.Size = new System.Drawing.Size(752, 278);
			this._panelTop.TabIndex = 0;
			// 
			// DrawingPad
			// 
			this.Controls.Add(this._panelTop);
			this.Controls.Add(this._splitterTopBottom);
			this.Controls.Add(this._componentTray);
			this.Name = "DrawingPad";
			this.Size = new System.Drawing.Size(752, 430);
			this._panelTop.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		
		#endregion

		
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[DesignOnly( true)]
		internal RulingPanel DrawingPanel
		{
			get{ return _rulingPanel;}
		}

		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[DesignOnly( true)]
		internal ShapeComponentTray ComponentTray
		{
			get{ return this._componentTray;}
		}

		private void InitializeComponentLTR()
		{
			this._panelTop.SuspendLayout();
			this.SuspendLayout();

			// _rulingPanel
			this._rulingPanel.Location = new System.Drawing.Point(20, 20);
			// _hRulerCtrl
			this._hRulerCtrl.Location = new System.Drawing.Point(19, -1);
			// _vRulerCtrl
			this._vRulerCtrl.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left)));
			this._vRulerCtrl.Location = new System.Drawing.Point(-1, 19);

			this._panelTop.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		

		private void InitializeComponentRTL()
		{
			this._panelTop.Dock = DockStyle.None;

			this._panelTop.SuspendLayout();
			this.SuspendLayout();


			// _rulingPanel
			this._rulingPanel.Location = new System.Drawing.Point(0, 20);
			// _hRulerCtrl
			this._hRulerCtrl.Location = new System.Drawing.Point(-1, -1);
			// _vRulerCtrl
			this._vRulerCtrl.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this._vRulerCtrl.Location = new System.Drawing.Point(this._panelTop.Width -20 , 19);


			this._panelTop.ResumeLayout(false);
			this.ResumeLayout(false);

			this._panelTop.Dock = DockStyle.Fill;


		}
		


		protected override void OnRightToLeftChanged(EventArgs e)
		{
			if( this.RightToLeft == RightToLeft.Yes) 
				InitializeComponentRTL();
			else InitializeComponentLTR();
			

			base.OnRightToLeftChanged (e);

		}




		private void ComponentTray_CloseClick(object sender, EventArgs e)
		{
			this.ComponentTray.Visible = false;



		}

		private void ComponentTray_VisibleChanged(object sender, EventArgs e)
		{
			this._splitterTopBottom.Height			 = this.ComponentTray.Visible ? 4 : 5;
			this._splitterTopBottom.DrawToggleButton = !this.ComponentTray.Visible;
		}

		private void SplitterTopBottom_DoubleClick(object sender, EventArgs e)
		{
			if( this.ComponentTray.Visible)
				this.ComponentTray.Visible = !this.ComponentTray.Visible ;

		}

		private void SplitterTopBottom_Click(object sender, EventArgs e)
		{
			if( !this.ComponentTray.Visible)
				this.ComponentTray.Visible = !this.ComponentTray.Visible ;
		}


		private class HeaderSplitter: System.Windows.Forms.Splitter
		{
			private bool _drawToggleButton= false;

			
			public HeaderSplitter()
			{
//				SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer, true);
//				SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, true);
				SetStyle(System.Windows.Forms.ControlStyles.ResizeRedraw, true);
//				SetStyle(System.Windows.Forms.ControlStyles.UserPaint, true);
		
			}


			[Browsable( false)]
			public bool DrawToggleButton
			{
				get{ return _drawToggleButton;}
				set
				{ 
					_drawToggleButton = value; 
					Invalidate();
				}
			}

			protected override void OnPaintBackground(PaintEventArgs pevent)
			{

				base.OnPaintBackground (pevent);
				
					Graphics g = pevent.Graphics;
					GraphicsContainer c = g.BeginContainer();
					try
					{
						g.SmoothingMode = SmoothingMode.AntiAlias;
						RectangleF bounds = this.ClientRectangle;
						float center = bounds.Width / 2f;
						if( _drawToggleButton )
						{
							PointF leftTop		= new PointF( center - 40, bounds.Top);
							PointF rightTop		= new PointF( center + 40, bounds.Top);
							PointF leftBottom	= new PointF( center - 50, bounds.Bottom);
							PointF rightBottom	= new PointF( center + 50, bounds.Bottom);
							PointF[] points = new PointF[]{ leftTop, rightTop, rightBottom, leftBottom};
							g.FillPolygon( SystemBrushes.ControlDarkDark,points);
							g.DrawPolygon( SystemPens.ControlDarkDark, points); 
						
							//Daaw bottom Line
							Pen pen = SystemPens.ControlDark.Clone() as Pen;
							pen.Width = 2f;//4f
							leftBottom	= new PointF( bounds.Left, bounds.Bottom);
							rightBottom	= new PointF( bounds.Right, bounds.Bottom);

							g.DrawLine( pen, leftBottom, rightBottom);

							leftTop		= new PointF( bounds.Left, bounds.Top);
							rightTop	= new PointF( bounds.Right, bounds.Top);
							g.DrawLine( pen, leftTop, rightTop);

						
						}
//						else
//						{
//							PointF leftTop		= new PointF( center - 50, bounds.Top);
//							PointF rightTop		= new PointF( center + 50, bounds.Top);
//							PointF leftBottom	= new PointF( center - 40, bounds.Bottom);
//							PointF rightBottom	= new PointF( center + 40, bounds.Bottom);
//							PointF[] points = new PointF[]{ leftTop, rightTop, rightBottom, leftBottom};
//							g.FillPolygon( SystemBrushes.ControlDarkDark,points);
//							g.DrawPolygon( SystemPens.ControlDarkDark, points); 
//						
//							//Daaw bottom Line
//							Pen pen = SystemPens.ControlDark.Clone() as Pen;
//							pen.Width = 2f;
//							leftTop		= new PointF( bounds.Left, bounds.Top);
//							rightTop	= new PointF( bounds.Right, bounds.Top);
//							g.DrawLine( pen, leftTop, rightTop);
//
//							leftBottom	= new PointF( bounds.Left, bounds.Bottom);
//							rightBottom	= new PointF( bounds.Right, bounds.Bottom);
//							g.DrawLine( pen, leftBottom, rightBottom);
//
//
//						}
					}
					finally
					{
						g.EndContainer(c);
					}
				

			}




		}

	}
}
